<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>尺码库存功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .test-container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
        }
        .test-title {
            color: #333;
            border-bottom: 2px solid #667eea;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }
        .api-test {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            margin: 10px 0;
        }
        .success {
            color: #28a745;
            font-weight: bold;
        }
        .error {
            color: #dc3545;
            font-weight: bold;
        }
        .loading {
            color: #007bff;
        }
        button {
            background: #667eea;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #5a6fd8;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
    <div class="test-container">
        <h1 class="test-title">尺码库存功能测试</h1>
        
        <div class="test-section">
            <h2>1. 测试尺码API</h2>
            <button onclick="testSizesAPI()">测试获取尺码列表</button>
            <div id="sizes-result" class="result"></div>
        </div>
        
        <div class="test-section">
            <h2>2. 测试库存API</h2>
            <button onclick="testStockAPI()">测试获取库存列表</button>
            <div id="stock-result" class="result"></div>
        </div>
        
        <div class="test-section">
            <h2>3. 测试商品API</h2>
            <button onclick="testProductsAPI()">测试获取商品列表</button>
            <div id="products-result" class="result"></div>
        </div>
        
        <div class="test-section">
            <h2>4. 测试添加库存</h2>
            <button onclick="testAddStock()">测试添加库存记录</button>
            <div id="add-stock-result" class="result"></div>
        </div>
        
        <div class="test-section">
            <h2>5. 测试查询特定商品库存</h2>
            <input type="number" id="productId" placeholder="商品ID" value="1">
            <button onclick="testProductStock()">查询商品库存</button>
            <div id="product-stock-result" class="result"></div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8080/api/v1';
        
        async function makeRequest(url, options = {}) {
            try {
                const response = await fetch(url, {
                    headers: {
                        'Content-Type': 'application/json',
                        ...options.headers
                    },
                    ...options
                });
                const data = await response.json();
                return { success: true, data, status: response.status };
            } catch (error) {
                return { success: false, error: error.message };
            }
        }
        
        async function testSizesAPI() {
            const resultDiv = document.getElementById('sizes-result');
            resultDiv.innerHTML = '<span class="loading">正在测试尺码API...</span>';
            
            const result = await makeRequest(`${API_BASE}/sizes/list`);
            
            if (result.success) {
                resultDiv.innerHTML = `<span class="success">✓ 尺码API测试成功</span>\n${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.innerHTML = `<span class="error">✗ 尺码API测试失败</span>\n错误: ${result.error}`;
            }
        }
        
        async function testStockAPI() {
            const resultDiv = document.getElementById('stock-result');
            resultDiv.innerHTML = '<span class="loading">正在测试库存API...</span>';
            
            const result = await makeRequest(`${API_BASE}/stock/list`);
            
            if (result.success) {
                resultDiv.innerHTML = `<span class="success">✓ 库存API测试成功</span>\n${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.innerHTML = `<span class="error">✗ 库存API测试失败</span>\n错误: ${result.error}`;
            }
        }
        
        async function testProductsAPI() {
            const resultDiv = document.getElementById('products-result');
            resultDiv.innerHTML = '<span class="loading">正在测试商品API...</span>';
            
            const result = await makeRequest(`${API_BASE}/shoes/list`);
            
            if (result.success) {
                resultDiv.innerHTML = `<span class="success">✓ 商品API测试成功</span>\n${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.innerHTML = `<span class="error">✗ 商品API测试失败</span>\n错误: ${result.error}`;
            }
        }
        
        async function testAddStock() {
            const resultDiv = document.getElementById('add-stock-result');
            resultDiv.innerHTML = '<span class="loading">正在测试添加库存...</span>';
            
            const stockData = {
                shoeId: 1,
                sizeId: 1,
                stockQuantity: 10
            };
            
            const result = await makeRequest(`${API_BASE}/stock/add`, {
                method: 'POST',
                body: JSON.stringify(stockData)
            });
            
            if (result.success) {
                resultDiv.innerHTML = `<span class="success">✓ 添加库存测试成功</span>\n${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.innerHTML = `<span class="error">✗ 添加库存测试失败</span>\n错误: ${result.error}`;
            }
        }
        
        async function testProductStock() {
            const resultDiv = document.getElementById('product-stock-result');
            const productId = document.getElementById('productId').value;
            
            if (!productId) {
                resultDiv.innerHTML = '<span class="error">请输入商品ID</span>';
                return;
            }
            
            resultDiv.innerHTML = '<span class="loading">正在查询商品库存...</span>';
            
            const result = await makeRequest(`${API_BASE}/stock/product/${productId}`);
            
            if (result.success) {
                resultDiv.innerHTML = `<span class="success">✓ 查询商品库存成功</span>\n${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.innerHTML = `<span class="error">✗ 查询商品库存失败</span>\n错误: ${result.error}`;
            }
        }
        
        // 页面加载时自动运行基础测试
        window.onload = function() {
            console.log('开始自动测试API连接...');
            testSizesAPI();
        };
    </script>
</body>
</html>